/*
 *    Copyright 2009-2010 The iBaGuice Team
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */
package com.google.code.ibaguice.samples;

import java.util.List;

import org.apache.ibatis.session.SqlSession;

import com.google.code.ibaguice.dao.AbstractDao;

/**
 * 
 * 
 * @author Marco Speranza
 * @version $Id: AbstractContactDao.java 000 2009-12-01 00:00:00Z marco.speranza79 $
 */
public abstract class AbstractContactDao extends AbstractDao implements ContactDao {

    private final Class<? extends ContactMapper> contactMapperType;

    /**
     * @param contactMapperType
     */
    public AbstractContactDao(Class<? extends ContactMapper> contactMapperType) {
        super();
        this.contactMapperType = contactMapperType;
    }

    /* (non-Javadoc)
     * @see com.google.code.ibaguice.samples.ContactDao#insert(com.google.code.ibaguice.samples.Contact)
     */
    public void insert(final Contact contact) throws DaoException {
        SqlSession session = this.getSqlSessionFactory().openSession();
        try {
            ContactMapper mapper = session.getMapper(this.contactMapperType);
            mapper.add(contact);
            session.commit();
        } catch (Exception e) {
            session.rollback();
            throw new DaoException("Impossible to store contact "
                    + contact
                    + ", see nested exceptions", e);
        } finally {
            session.close();
        }
    }

    /* (non-Javadoc)
     * @see com.google.code.ibaguice.samples.ContactDao#delete(java.lang.Integer)
     */
    public void delete(final Integer id) throws DaoException {
        SqlSession session = this.getSqlSessionFactory().openSession();
        try {
            ContactMapper mapper = session.getMapper(this.contactMapperType);
            mapper.delete(id);
            session.commit(true);
        } catch (Exception e) {
            session.rollback();
            throw new DaoException("Impossible to delete contact "
                    + id
                    + ", see nested exceptions", e);
        } finally {
            session.close();
        }
    }

    /* (non-Javadoc)
     * @see com.google.code.ibaguice.samples.ContactDao#selectById(java.lang.Integer)
     */
    public Contact selectById(final Integer id) throws DaoException {
        SqlSession session = this.getSqlSessionFactory().openSession();
        try {
            ContactMapper mapper = session.getMapper(this.contactMapperType);
            return mapper.getById(id);
        } catch (Exception e) {
            session.rollback();
            throw new DaoException("Impossible to retrieve contact "
                    + id
                    + ", see nested exceptions", e);
        } finally {
            session.close();
        }
    }

    /* (non-Javadoc)
     * @see com.google.code.ibaguice.samples.ContactDao#getAll()
     */
    public List<Contact> getAll() throws DaoException {
        SqlSession session = this.getSqlSessionFactory().openSession();
        try {
            ContactMapper mapper = session.getMapper(this.contactMapperType);
            List<Contact> res = mapper.selectAll();
            session.commit(true);
            return res;
        } catch (Exception e) {
            session.rollback();
            throw new DaoException("Impossible to retrieve all contacts, see nested exceptions", e);
        } finally {
            session.close();
        }
    }

    /* (non-Javadoc)
     * @see com.google.code.ibaguice.samples.ContactDao#update(com.google.code.ibaguice.samples.Contact)
     */
    public void update(final Contact contact) throws DaoException {
        SqlSession session = this.getSqlSessionFactory().openSession();
        try {
            ContactMapper mapper = session.getMapper(this.contactMapperType);
            mapper.update(contact);
            session.commit();
        } catch (Exception e) {
            session.rollback();
            throw new DaoException("Impossible to update contact "
                    + contact
                    + ", see nested exceptions", e);
        } finally {
            session.close();
        }
    }

}
